package com.design.twords.adatper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.design.twords.R;
import com.design.twords.entity.ItemWordBook;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by Alen
 * Time:  23:29
 */
public class ChangePlanAdapter extends RecyclerView.Adapter<ChangePlanAdapter.MyViewHolder>{

    public interface ItemClickListener{
        void itemClickListener(int position);
    }

    private ItemClickListener listener;



    private LayoutInflater myInflater;
    private ArrayList<ItemWordBook> wordBooks;
    private Context context;
    private int viewId;

    public ChangePlanAdapter(ArrayList<ItemWordBook> wordBooks, Context context, int viewId) {
        this.myInflater = LayoutInflater.from(context);
        this.wordBooks = wordBooks;
        this.context = context;
        this.viewId = viewId;
    }

    public void setListener(ItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(myInflater.inflate(viewId, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ItemWordBook item = wordBooks.get(position);
        holder.imageView.setImageResource(item.getBookImg());
        holder.bookName.setText(item.getBookName());
        holder.bookRes.setText(item.getBookSource());
        holder.bookCounts.setText(item.getBookWordNum()+"");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.itemClickListener(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return wordBooks.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView bookName;
        private TextView bookRes;
        private TextView bookCounts;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = (ImageView)itemView.findViewById(R.id.book_pic);
            bookName = itemView.findViewById(R.id.book_name);
            bookRes = itemView.findViewById(R.id.book_res);
            bookCounts = itemView.findViewById(R.id.book_wordcounts);
        }
    }
}
